﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using NUnit.Framework;
using ImapX.Parsing.Commands;
using ImapX.Parsing.DataItems;
using ImapX.Parsing.Tokens;

namespace ImapX.Test
{
    /// <summary>
    /// Summary description for ImapX.
    /// </summary>
    [TestFixture]
    public class ImapXTest
    {
        /// <summary>
        /// Perform generic test initialization. Because this method is
        /// marked with the <code>SetUp</code> attribute, it is called
        /// prior to the invocation of each test.
        /// </summary>
        [SetUp]
        public void Init()
        {
        }

        /// <summary>
        /// Perform generic post-test cleanup. Because this method is
        /// marked with the <code>tearDown</code> attribute, it is called
        /// following the completion of each test method.
        /// </summary>
        [TearDown]
        public void Dispose()
        {
        }

        /// <summary>
        /// Description of Test1.
        /// </summary>
        [TestCase("FETCH")]
        [ExpectedException(typeof(ImapException))]
        public void ParseFetchInternalDate(string input)
        {
            var date = new InternalDate();
            var fetch = new UidFetch(1, date);
            fetch.Parse(input);
        }

        [TestCase("* 1 FETCH (UID 9103 ENVELOPE (\"Mon, 6 Nov 2006 11:34:28 -0500\" \"FW: PO# 2735\" ((\"Bob Smith\" NIL \"bob\" \"somewhere.com\")) ((\"Bob Smith\" NIL \"bob\" \"somewhere.com\")) ((\"Bob Smith\" NIL \"bob\" \"somewhere.com\")) ((NIL NIL \"John\" \"somewhereelse.com\")) NIL NIL NIL \"<GCEJKFGLFKKBFGGODCMLIEDIDPAA.bob@somewhere.com>\"))\r\nIMAP0012 OK Success\r\n")]
        public void ParseFetchEnvelope(string input)
        {
            var envelope = new Envelope();
            var fetch = new UidFetch(9103, envelope);
            fetch.Parse(input);
        }

        [TestCase("ENVELOPE (\"Thu, 23 Sep 2010 10:41:58 +0800 (MSD)\" \"=?KOI8-R?B?8MXSxcTB1Ngg1yDP1MTFzCDSxcvMwc3Z?=\" ((\"=?KOI8-R?B?8MHIz83P1w==?=\" NIL \"lampooningw66\" \"block-h.ru\")) ((\"=?KOI8-R?B?8MHIz83P1w==?=\" NIL \"lampooningw66\" \"block-h.ru\")) ((NIL NIL \"signatoryno23\" \"byenotebook.ru\\\")) ((NIL NIL \"inbox\" \"luminosindustries.com\")) NIL NIL NIL \"<20100923104158.1CLP1931P23@block-h.ru>\"))")]
        [TestCase("ENVELOPE (\"Mon, 6 Nov 2006 11:34:28 -0500\" \"FW: PO# 2735\" ((\"Bob Smith\" NIL \"bob\" \"somewhere.com\")) ((\"Bob Smith\" NIL \"bob\" \"somewhere.com\")) ((\"Bob Smith\" NIL \"bob\" \"somewhere.com\")) ((NIL NIL \"John\" \"somewhereelse.com\")) NIL NIL NIL \"<GCEJKFGLFKKBFGGODCMLIEDIDPAA.bob@somewhere.com>\"))")]
        public void ParseEnvelope(string input)
        {
            var envelope = new Envelope();
            envelope.Parse(input);
        }

        [TestCase("((\"Bob Smith\" NIL \"bob\" \"somewhere.com\"))", "Bob Smith", @"""Bob Smith"" <bob@somewhere.com>")]
        [TestCase("((\"John Doe\" NIL \"john\" \"somewhereelse.com\") (\"Bob Smith\" NIL \"bob\" \"somewhere.com\"))", "John Doe", @"""John Doe"" <john@somewhereelse.com>")]
        public void ParseAddressList(string input, string name, string email)
        {
            var addresslist = new NList<Address>();
            addresslist.Parse(input);
            Assert.AreEqual(addresslist[0].Name, name);
            Assert.AreEqual(addresslist[0].Email, email);
        }

        [TestCase("(\"Bob Smith\" NIL \"bob\" \"somewhere.com\")", "Bob Smith", @"""Bob Smith"" <bob@somewhere.com>")]
        public void ParseAddress(string input, string name, string email)
        {
            var address = new Address();
            address.Parse(input);
            Assert.AreEqual(address.Name, name);
            Assert.AreEqual(address.Email, email);
        }

        [TestCase(@"""Bob Smith""", "Bob Smith")]
        //[TestCase(@"""This \""is\"" a test""", @"This \""is\"" a test")]
        public void ParseNString(string input, string output)
        {
            var nstring = new NString();
            nstring.Parse(input);

            Assert.AreEqual(nstring.Value, output);
        }

        [TestCase(@"FLAGS (\Seen \Draft)", @"\Seen")]
        public void ParseFlags(string input, string output)
        {
            var flags = new Flags();
            flags.Parse(input);

            Assert.AreEqual(flags[0], output);
        }
    }
}